package com.surpermaket.controller;

import com.alibaba.fastjson.JSON;
import com.surpermaket.entity.Menu;
import com.surpermaket.entity.Role;
import com.surpermaket.entity.User;
import com.surpermaket.service.MenuService;
import com.surpermaket.service.RoleService;
import com.surpermaket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userServiceImpl;

    @RequestMapping("/loginAction")
    //ResponseBody表示接受ajax请求
    @ResponseBody
    public Object loginAction(String username, String password, String code,HttpSession session){
        System.out.println(username);
        System.out.println(password);
        String scode = (String)session.getAttribute("scode");
        //使用map防止乱码
        Map<String,Object> map = new HashMap<>();
        if(!scode.equals(code)){
            map.put("message","验证码错误");
            return JSON.toJSON(map);
        }

        //验证登陆名称和密码
        User user = userServiceImpl.login(username, password);
        if(user!=null){
            //将登陆的用户信息，添加到session中
            session.setAttribute("userInfo",user);
            map.put("message", "success");
        }else{
            map.put("message", "用户名或密码错误");
        }
        return JSON.toJSON(map);
    }


    @RequestMapping("/registAction")
    @ResponseBody
    public Object registAction(String username, String password, String code, HttpSession session){
        System.out.println(username);
        System.out.println(password);
        String scode = (String)session.getAttribute("scode");
        //使用map防止乱码
        Map<String,Object> map = new HashMap<>();
        if(!scode.equals(code)){
            map.put("message","验证码错误");
            return JSON.toJSON(map);
        }

        //验证注册名称和密码
        int result = userServiceImpl.regist(username, password);
        if(result >0){
            map.put("message", "注册成功");
        }else{
            map.put("message", "注册失败");
        }
        return JSON.toJSON(map);
    }

    @Autowired
    private MenuService menuServiceImpl;
    @RequestMapping("/index")
    public String index(Model model){
        //查询所有菜单，并返回
        List<Menu> menus = menuServiceImpl.selectOne();
        Menu n = new Menu();
        model.addAttribute("menus",menus);
        return "index";
    }
    @RequestMapping("user/list")
    public String List(Model model,String username){
        User user = new User();
        user.setUsername(username);
        List<User> list  = userServiceImpl.selectAll(user);
        model.addAttribute("list",list);
        return "list"; //jsp文件名称
    }

    @Autowired
    private RoleService roleServiceImpl;
    @RequestMapping("user/add")
    public String add(Model model){
        List<Role> roles  = roleServiceImpl.selectAll(new Role());
        model.addAttribute("list",roles);
        return "add"; //jsp文件名称
    }

    @RequestMapping("user/insert")
    public String insert(User user){
        user.setCreatetime(new Date());
        userServiceImpl.insert(user);
        return "redirect:list"; //jsp文件名称
    }


}
